package com.dianping.cat.alarm.server.transform;

import com.dianping.cat.alarm.server.IEntity;
import com.dianping.cat.alarm.server.IVisitor;
import com.dianping.cat.alarm.server.entity.Condition;
import com.dianping.cat.alarm.server.entity.Rule;
import com.dianping.cat.alarm.server.entity.ServerAlarmRuleConfig;
import com.dianping.cat.alarm.server.entity.SubCondition;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

















public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  37 */   public DefaultJsonBuilder() { this(false); }



/*  41 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  45 */     this.m_compact = compact;
/*  46 */     this.m_sb = sb;
/*  47 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  51 */     indent();
/*  52 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  53 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  57 */     this.m_level--;

/*  59 */     trimComma();
/*  60 */     indent();
/*  61 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  65 */     int len = nameValues.length;

/*  67 */     for (int i = 0; i + 1 < len; i += 2) {
/*  68 */       Object attrName = nameValues[i];
/*  69 */       Object attrValue = nameValues[i + 1];

/*  71 */       if (attrValue != null) {
/*  72 */         if (attrValue instanceof Collection) {

/*  74 */           Collection<Object> items = (Collection)attrValue;

/*  76 */           if (!items.isEmpty()) {
/*  77 */             indent();
            this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  80 */             for (Object item : items) {
/*  81 */               this.m_sb.append(' ');
/*  82 */               toString(this.m_sb, item);
/*  83 */               this.m_sb.append(',');
            }

/*  86 */             this.m_sb.setLength(this.m_sb.length() - 1);
/*  87 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  89 */         } else if (attrValue.getClass().isArray()) {
/*  90 */           int length = Array.getLength(attrValue);

/*  92 */           if (length > 0) {
/*  93 */             indent();
/*  94 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  96 */             for (int j = 0; j < length; j++) {
/*  97 */               Object item = Array.get(attrValue, j);
/*  98 */               this.m_sb.append(' ');
/*  99 */               toString(this.m_sb, item);
/* 100 */               this.m_sb.append(',');
            }

/* 103 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 104 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/* 107 */         } else if (this.m_compact) {
/* 108 */           this.m_sb.append('"').append(attrName).append("\":");
/* 109 */           toString(this.m_sb, attrValue);
/* 110 */           this.m_sb.append(",");
        } else {
/* 112 */           indent();
/* 113 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 114 */           toString(this.m_sb, attrValue);
/* 115 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 121 */     if (dynamicAttributes != null) {
/* 122 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 123 */         if (this.m_compact) {
/* 124 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 125 */           toString(this.m_sb, e.getValue());
/* 126 */           this.m_sb.append(","); continue;
        }
/* 128 */         indent();
/* 129 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 130 */         toString(this.m_sb, e.getValue());
/* 131 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 138 */     objectBegin(null);
/* 139 */     entity.accept(this);
/* 140 */     objectEnd(null);
/* 141 */     trimComma();

/* 143 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 147 */     this.m_sb.append('[');

/* 149 */     if (entities != null) {
/* 150 */       for (IEntity<?> entity : entities) {
/* 151 */         objectBegin(null);
/* 152 */         entity.accept(this);
/* 153 */         objectEnd(null);
      }

/* 156 */       trimComma();
    }

/* 159 */     this.m_sb.append(']');

/* 161 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 165 */     if (!this.m_compact) {
/* 166 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 167 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 173 */     indent();

/* 175 */     if (name == null) {
/* 176 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 178 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 181 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 185 */     this.m_level--;

/* 187 */     trimComma();
/* 188 */     indent();
/* 189 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected void toString(StringBuilder sb, Object value) {
/* 193 */     if (value == null) {
/* 194 */       sb.append("null");
/* 195 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 196 */       sb.append(value);
    } else {
/* 198 */       String val = value.toString();
/* 199 */       int len = val.length();

/* 201 */       sb.append('"');

/* 203 */       for (int i = 0; i < len; i++) {
/* 204 */         char ch = val.charAt(i);

/* 206 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 210 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 213 */             sb.append("\\t");
            break;
          case '\r':
/* 216 */             sb.append("\\r");
            break;
          case '\n':
/* 219 */             sb.append("\\n");
            break;
          default:
/* 222 */             sb.append(ch);
            break;
        }

      }
/* 227 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 232 */     int len = this.m_sb.length();

/* 234 */     if (this.m_compact) {
/* 235 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 236 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 239 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 240 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }



  public void visitCondition(Condition condition) {
/* 247 */     attributes(null, new Object[] { "interval", condition.getInterval(), "duration", Integer.valueOf(condition.getDuration()), "alert-type", condition.getAlertType() });

/* 249 */     if (!condition.getSubConditions().isEmpty()) {
/* 250 */       arrayBegin("sub-conditions");

/* 252 */       for (SubCondition subCondition : condition.getSubConditions()) {
/* 253 */         objectBegin(null);
/* 254 */         subCondition.accept(this.m_visitor);
/* 255 */         objectEnd(null);
      }

/* 258 */       arrayEnd("sub-conditions");
    }
  }


  public void visitRule(Rule rule) {
/* 264 */     attributes(null, new Object[] { "start-time", rule.getStartTime(), "end-time", rule.getEndTime() });

/* 266 */     if (!rule.getConditions().isEmpty()) {
/* 267 */       arrayBegin("conditions");

/* 269 */       for (Condition condition : rule.getConditions()) {
/* 270 */         objectBegin(null);
/* 271 */         condition.accept(this.m_visitor);
/* 272 */         objectEnd(null);
      }

/* 275 */       arrayEnd("conditions");
    }
  }


  public void visitServerAlarmRuleConfig(ServerAlarmRuleConfig serverAlarmRuleConfig) {
/* 281 */     attributes(null, new Object[] { "id", serverAlarmRuleConfig.getId() });

/* 283 */     if (!serverAlarmRuleConfig.getRules().isEmpty()) {
/* 284 */       arrayBegin("rules");

/* 286 */       for (Rule rule : serverAlarmRuleConfig.getRules()) {
/* 287 */         objectBegin(null);
/* 288 */         rule.accept(this.m_visitor);
/* 289 */         objectEnd(null);
      }

/* 292 */       arrayEnd("rules");
    }
  }



/* 298 */   public void visitSubCondition(SubCondition subCondition) { attributes(null, new Object[] { "type", subCondition.getType(), "value", subCondition.getValue() }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/server/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */